﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class EnumExtension
    {
        /// <summary>
        /// Converts the string representation of an entity to Microsoft.Crm.SdkTypeProxy.EntityName enumerated object.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static Microsoft.Crm.SdkTypeProxy.EntityName ToCrm4EntityName(this String entity)
        {
            Microsoft.Crm.SdkTypeProxy.EntityName entityName;

            if (!Enum.TryParse<Microsoft.Crm.SdkTypeProxy.EntityName>(entity, true, out entityName))
                throw new Exception(String.Format("Entity name: {0} cannot be converted to Microsoft.Crm.SdkTypeProxy.EntityName object", entityName));

            return entityName;
        }

        public static Microsoft.Crm.Sdk.PropagationOwnershipOptions ToCrm4PropagationOwnershipOptions(this Microsoft.Crm.Sdk.Messages.PropagationOwnershipOptions crm2011PropagationOwnershipOptions)
        {
            Microsoft.Crm.Sdk.PropagationOwnershipOptions crm4PropagationOwnershipOptions;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.PropagationOwnershipOptions>(crm2011PropagationOwnershipOptions.ToString(), true, out crm4PropagationOwnershipOptions))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.PropagationOwnershipOptions.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.PropagationOwnershipOptions.", crm2011PropagationOwnershipOptions.ToString()));

            return crm4PropagationOwnershipOptions;
        }

        public static Microsoft.Crm.Sdk.AccessRights ToCrm4AccessRights(this Microsoft.Crm.Sdk.Messages.AccessRights crm2011AccessRights)
        {
            Microsoft.Crm.Sdk.AccessRights crm4AccessRights;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.AccessRights>(crm2011AccessRights.ToString(), true, out crm4AccessRights))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.AccessRights.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.AccessRights.", crm2011AccessRights.ToString()));

            return crm4AccessRights;
        }

        public static Microsoft.Crm.Sdk.Messages.AccessRights ToCrm2011AccessRights(this Microsoft.Crm.Sdk.AccessRights crm4AccessRights)
        {
            Microsoft.Crm.Sdk.Messages.AccessRights crm2011AccessRights;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Messages.AccessRights>(crm4AccessRights.ToString(), true, out crm2011AccessRights))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.AccessRights.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Messages.AccessRights.", crm4AccessRights.ToString()));

            return crm2011AccessRights;
        }

        public static Microsoft.Crm.Sdk.TargetFieldType ToCrm4TargetFieldType(this Microsoft.Crm.Sdk.Messages.TargetFieldType crm2011TargetFieldType)
        {
            Microsoft.Crm.Sdk.TargetFieldType crm4TargetFieldType;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.TargetFieldType>(crm2011TargetFieldType.ToString(), true, out crm4TargetFieldType))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.TargetFieldType.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.TargetFieldType.", crm2011TargetFieldType.ToString()));

            return crm4TargetFieldType;
        }

        public static Microsoft.Crm.Sdk.TimeCode ToCrm4TimeCode(this Microsoft.Crm.Sdk.Messages.TimeCode crm2011TimeCode)
        {
            Microsoft.Crm.Sdk.TimeCode crm4TimeCode;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.TimeCode>(crm2011TimeCode.ToString(), true, out crm4TimeCode))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.TimeCode.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.TimeCode.", crm2011TimeCode.ToString()));

            return crm4TimeCode;
        }

        public static Microsoft.Crm.Sdk.Messages.TimeCode ToCrm2011TimeCode(this Microsoft.Crm.Sdk.TimeCode crm4TimeCode)
        {
            Microsoft.Crm.Sdk.Messages.TimeCode crm2011TimeCode;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Messages.TimeCode>(crm4TimeCode.ToString(), true, out crm2011TimeCode))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.TimeCode.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Messages.TimeCode.", crm4TimeCode.ToString()));

            return crm2011TimeCode;
        }

        public static Microsoft.Crm.Sdk.TimeCode[] ToCrm4TimeCodeArray(this Microsoft.Crm.Sdk.Messages.TimeCode[] crm2011TimeCodes)
        {
            Microsoft.Crm.Sdk.TimeCode[] crm4TimeCode = new Microsoft.Crm.Sdk.TimeCode[crm2011TimeCodes.Length];
            for (int i = 0; i < crm2011TimeCodes.Length; i++)
            {
                crm4TimeCode[i] = crm2011TimeCodes[i].ToCrm4TimeCode();
            }

            return crm4TimeCode;
        }

        public static Microsoft.Crm.Sdk.RollupType ToCrm4RollupType(this Microsoft.Crm.Sdk.Messages.RollupType crm2011RollupType)
        {
            Microsoft.Crm.Sdk.RollupType crm4RollupType;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.RollupType>(crm2011RollupType.ToString(), true, out crm4RollupType))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.RollupType.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.RollupType.", crm2011RollupType.ToString()));

            return crm4RollupType;
        }

        public static Microsoft.Crm.Sdk.SearchDirection ToCrm4SearchDirection(this Microsoft.Crm.Sdk.Messages.SearchDirection crm2011SearchDirection)
        {
            Microsoft.Crm.Sdk.SearchDirection crm4SearchDirection;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.SearchDirection>(crm2011SearchDirection.ToString(), true, out crm4SearchDirection))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.SearchDirection.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.SearchDirection.", crm2011SearchDirection.ToString()));

            return crm4SearchDirection;
        }

        public static Microsoft.Crm.Sdk.Messages.SubCode ToCrm2011SubCode(this Microsoft.Crm.Sdk.SubCode crm4SubCode)
        {
            Microsoft.Crm.Sdk.Messages.SubCode crm2011SubCode;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Messages.SubCode>(crm4SubCode.ToString(), true, out crm2011SubCode))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.SearchDirection.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Messages.SubCode.", crm4SubCode.ToString()));

            return crm2011SubCode;
        }

        public static Microsoft.Crm.Sdk.PrivilegeDepth ToCrm4PrivilegeDepth(this Microsoft.Crm.Sdk.Messages.PrivilegeDepth crm2011PrivilegeDepth)
        {
            Microsoft.Crm.Sdk.PrivilegeDepth crm4PrivilegeDepth;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.PrivilegeDepth>(crm2011PrivilegeDepth.ToString(), true, out crm4PrivilegeDepth))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.Messages.PrivilegeDepth.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.PrivilegeDepth.", crm2011PrivilegeDepth.ToString()));

            return crm4PrivilegeDepth;
        }

        public static Microsoft.Crm.Sdk.Messages.PrivilegeDepth ToCrm2011PrivilegeDepth(this Microsoft.Crm.Sdk.PrivilegeDepth crm4PrivilegeDepth)
        {
            Microsoft.Crm.Sdk.Messages.PrivilegeDepth crm2011PrivilegeDepth;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Messages.PrivilegeDepth>(crm4PrivilegeDepth.ToString(), true, out crm2011PrivilegeDepth))
                throw new Exception(String.Format("This Microsoft.Crm.Sdk.PrivilegeDepth.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Messages.PrivilegeDepth.", crm4PrivilegeDepth.ToString()));

            return crm2011PrivilegeDepth;
        }

        public static string ToStringExtended(this Microsoft.Crm.Sdk.SecurityPrincipalType crm4SecurityPrincipalType)
        {
            string securityPrincipalType = string.Empty;

            securityPrincipalType =
                (crm4SecurityPrincipalType == Microsoft.Crm.Sdk.SecurityPrincipalType.User ? "systemuser" : "team");

            return securityPrincipalType;
        }

        public static Microsoft.Crm.Sdk.Query.ConditionOperator ToCrm4ConditionOperator(this Microsoft.Xrm.Sdk.Query.ConditionOperator crm2011ConditionOperator)
        {
            Microsoft.Crm.Sdk.Query.ConditionOperator crm4ConditionOperator;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Query.ConditionOperator>(crm2011ConditionOperator.ToString(), true, out crm4ConditionOperator))
                throw new Exception(String.Format("Microsoft.Xrm.Sdk.Query.ConditionOperator.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Query.ConditionOperator.", crm2011ConditionOperator.ToString()));

            return crm4ConditionOperator;
        }

        public static Microsoft.Xrm.Sdk.Query.ConditionOperator ToCrm2011ConditionOperator(this Microsoft.Crm.Sdk.Query.ConditionOperator crm4ConditionOperator)
        {
            Microsoft.Xrm.Sdk.Query.ConditionOperator crm2011ConditionOperator;

            if (!Enum.TryParse<Microsoft.Xrm.Sdk.Query.ConditionOperator>(crm4ConditionOperator.ToString(), true, out crm2011ConditionOperator))
                throw new Exception(String.Format("Microsoft.Crm.Sdk.Query.ConditionOperator.{0} cannot be converted to an enumerated type of Microsoft.Xrm.Sdk.Query.ConditionOperator.", crm4ConditionOperator.ToString()));

            return crm2011ConditionOperator;
        }

        public static Microsoft.Crm.Sdk.Query.LogicalOperator ToCrm4LogicalOperator(this Microsoft.Xrm.Sdk.Query.LogicalOperator crm2011LogicalOperator)
        {
            Microsoft.Crm.Sdk.Query.LogicalOperator crm4LogicalOperator;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Query.LogicalOperator>(crm2011LogicalOperator.ToString(), true, out crm4LogicalOperator))
                throw new Exception(String.Format("Microsoft.Xrm.Sdk.Query.LogicalOperator.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Query.LogicalOperator.", crm2011LogicalOperator.ToString()));

            return crm4LogicalOperator;
        }

        public static Microsoft.Xrm.Sdk.Query.LogicalOperator ToCrm2011LogicalOperator(this Microsoft.Crm.Sdk.Query.LogicalOperator crm4LogicalOperator)
        {
            Microsoft.Xrm.Sdk.Query.LogicalOperator crm2011LogicalOperator;

            if (!Enum.TryParse<Microsoft.Xrm.Sdk.Query.LogicalOperator>(crm4LogicalOperator.ToString(), true, out crm2011LogicalOperator))
                throw new Exception(String.Format("Microsoft.Crm.Sdk.Query.LogicalOperator.{0} cannot be converted to an enumerated type of Microsoft.Xrm.Sdk.Query.LogicalOperator.", crm4LogicalOperator.ToString()));

            return crm2011LogicalOperator;
        }

        public static Microsoft.Crm.Sdk.Query.OrderType ToCrm4OrderType(this Microsoft.Xrm.Sdk.Query.OrderType crm2011OrderType)
        {
            Microsoft.Crm.Sdk.Query.OrderType crm4OrderType;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Query.OrderType>(crm2011OrderType.ToString(), true, out crm4OrderType))
                throw new Exception(String.Format("Microsoft.Xrm.Sdk.Query.OrderType.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Query.OrderType.", crm2011OrderType.ToString()));

            return crm4OrderType;
        }

        public static Microsoft.Xrm.Sdk.Query.OrderType ToCrm2011OrderType(this Microsoft.Crm.Sdk.Query.OrderType crm4OrderType)
        {
            Microsoft.Xrm.Sdk.Query.OrderType crm2011OrderType;

            if (!Enum.TryParse<Microsoft.Xrm.Sdk.Query.OrderType>(crm4OrderType.ToString(), true, out crm2011OrderType))
                throw new Exception(String.Format("Microsoft.Crm.Sdk.Query.OrderType.{0} cannot be converted to an enumerated type of Microsoft.Xrm.Sdk.Query.OrderType.", crm4OrderType.ToString()));

            return crm2011OrderType;
        }

        public static Microsoft.Crm.Sdk.Query.JoinOperator ToCrm4JoinOperator(this Microsoft.Xrm.Sdk.Query.JoinOperator crm2011JoinOperator)
        {
            Microsoft.Crm.Sdk.Query.JoinOperator crm4JoinOperator;

            if (!Enum.TryParse<Microsoft.Crm.Sdk.Query.JoinOperator>(crm2011JoinOperator.ToString(), true, out crm4JoinOperator))
                throw new Exception(String.Format("Microsoft.Xrm.Sdk.Query.JoinOperator.{0} cannot be converted to an enumerated type of Microsoft.Crm.Sdk.Query.JoinOperator.", crm2011JoinOperator.ToString()));

            return crm4JoinOperator;
        }

        public static Microsoft.Xrm.Sdk.Query.JoinOperator ToCrm2011JoinOperator(this Microsoft.Crm.Sdk.Query.JoinOperator crm4JoinOperator)
        {
            Microsoft.Xrm.Sdk.Query.JoinOperator crm2011JoinOperator;

            if (!Enum.TryParse<Microsoft.Xrm.Sdk.Query.JoinOperator>(crm4JoinOperator.ToString(), true, out crm2011JoinOperator))
                throw new Exception(String.Format("Microsoft.Crm.Sdk.Query.JoinOperator.{0} cannot be converted to an enumerated type of Microsoft.Xrm.Sdk.Query.JoinOperator.", crm4JoinOperator.ToString()));

            return crm2011JoinOperator;
        }
    }
}
